package com.taotao.sso.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import com.taotao.common.CookieUtils;
import com.taotao.common.JsonUtils;
import com.taotao.common.TaotaoResult;
import com.taotao.mapper.TbUserMapper;
import com.taotao.pojo.TbUser;
import com.taotao.pojo.TbUserExample;
import com.taotao.pojo.TbUserExample.Criteria;
import com.taotao.sso.redis.JedisClient;
import com.taotao.sso.service.UserService;

@Service
public class UserServiceImpl implements UserService{
	@Autowired
	private TbUserMapper userMapper;
	
	@Autowired
	private JedisClient jedisClient;
	
	@Value("${SSO_TOKEN_SESSION_KEY}")
	private String SSO_TOKEN_SESSION_KEY;
	@Value("${SSO_TOKEN_SESSION_EXPIRE}")
	private Integer SSO_TOKEN_SESSION_EXPIRE;
	//校验注册信息
	@Override
	public TaotaoResult checkDate(String param,Integer type) {
		TbUserExample userExample=new TbUserExample();
		Criteria criteria = userExample.createCriteria();
		if(type==1){
			criteria.andUsernameEqualTo(param);
		}
		if(type==2){
			criteria.andPhoneEqualTo(param);
		}
		if(type==3){
			criteria.andEmailEqualTo(param);
		}
		List<TbUser> users = userMapper.selectByExample(userExample);
		if(users!=null&&users.size()>0){
			return TaotaoResult.ok(false);
		}
		return TaotaoResult.ok(true);
	}

	@Override
	public TaotaoResult createUser(TbUser user) {
		user.setCreated(new Date());
		user.setUpdated(new Date());
		user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
		userMapper.insert(user);
		return TaotaoResult.ok();
	}

	@Override
	public TaotaoResult userLogin(String username, String password,
			HttpServletRequest request,HttpServletResponse response) {
		TbUserExample userExample=new TbUserExample();
		Criteria criteria = userExample.createCriteria();
		criteria.andUsernameEqualTo(username);
		List<TbUser> users = userMapper.selectByExample(userExample);
		if(users==null||users.size()==0){
			return TaotaoResult.build(400, "用户名或密码不存在！");
		}
		TbUser user=users.get(0);
		if(!DigestUtils.md5DigestAsHex(password.getBytes()).equals(user.getPassword())){
			return TaotaoResult.build(400, "用户名或密码不匹配!");
		}
		//将用户信息存到redis中
		user.setPassword(null);
		String token=UUID.randomUUID().toString();
		jedisClient.set(SSO_TOKEN_SESSION_KEY+":"+token, JsonUtils.objectToJson(user));
		jedisClient.expire(SSO_TOKEN_SESSION_KEY+":"+token, SSO_TOKEN_SESSION_EXPIRE);
		CookieUtils.setCookie(request, response, "TT_TOKEN", token);
		return TaotaoResult.ok(token);
	}

	@Override
	public TaotaoResult getUserByToken(String token) {
		String json=jedisClient.get(SSO_TOKEN_SESSION_KEY+":"+token);
		if(StringUtils.isBlank(json)){
			return TaotaoResult.build(500, "session过期，请重新登入!");
		}
		//更新session的过期时间
		jedisClient.expire(SSO_TOKEN_SESSION_KEY+":"+token, SSO_TOKEN_SESSION_EXPIRE);
		//返回用户信息
		return TaotaoResult.ok(JsonUtils.jsonToPojo(json, TbUser.class));
	}
}
